#ifndef PROXY_CONN_TCPSERVER_H_
#define PROXY_CONN_TCPSERVER_H_

#include "proxy/base/proxybase.h"
#include "proxy/conn/tcpaccpetserver.h"
#include "proxy/conn/flowinstance.h"

namespace proxy{

  class TcpServer : public boost::noncopyable,
    public boost::enable_shared_from_this < TcpServer > {
  public:
    typedef boost::shared_ptr<TcpServer> Ptr;
    TcpServer(std::string bind_addr, uint16 bind_port, int thread_num);
    virtual ~TcpServer();
    bool Start();
    void Stop();
    FlowInstance::Ptr flow_instance() { return flow_instance_; }
  private:
    void RunAccpetServer(io_service_ptr io_service);
    void RunThread(io_service_ptr io_service);
    void OnServerError(TcpAccpetServer::Ptr accpet_server,
      const boost::system::error_code& err);
  protected:
    virtual void OnNewConnect(TcpAccpetServer::Ptr accpet_server,
      socket_ptr connect) = 0;
  private:
    TcpAccpetServer::Ptr accpet_server_;
    ios_deque io_services_;
    boost::thread_group thr_grp_;
    std::deque<boost::asio::io_service::work> io_service_work_;
    boost::shared_ptr<boost::asio::io_service> accpet_service_;
    bool is_stop_;
    int thread_num_;
    std::string bind_addr_;
    uint16 bind_port_;
    FlowInstance::Ptr flow_instance_;
  };
} // namespace proxy

#endif // PROXY_CONN_TCPSERVER_H_